const crypto = require('crypto');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
    username: String,
    password: String,
    email: String,
    salt: String,
    created: {
        type: Date,
        default: Date.now
    },
    followers: [{
        type: Schema.Types.ObjectId,
        ref: 'User'
    }],
    followersCount: {
        type: Number,
        default: 0
    },
    followingCount: {
        type: Number,
        default: 0
    }
});

UserSchema.methods.hashPassword = function (password) {
    //crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)
    return crypto.pbkdf2Sync(password, this.salt, 10000, 64, 'sha512').toString('base64');
};

UserSchema.methods.verifyPassword = function (password) {
    return this.password === this.hashPassword(password);
};
mongoose.model('User', UserSchema);